<?php
namespace App\Exports;

use App\Http\Services\CommonManager;
use App\Merchant;
use App\OrderAddress;
use App\Orders;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithMapping;

class OrdersExport implements FromArray
{
    protected $orderId = [];

    public function __construct($orderId)
    {
        $this->orderId = $orderId;
    }

    public function array(): array
    {
        $data = [];
        $temp=[
            '订单号','订单状态','下单用户','实付','所属门店','配送方式','创建时间','支付时间','收货人','收货人手机号','收货人地址','发货时间'
        ];

        array_push($data,$temp);

        /** @var Orders $order */
        $orderList = Orders::whereIn('id',$this->orderId)->get();
        if(count($orderList) > 0){
            foreach ($orderList as $order) {
                $userInfo = CommonManager::getUserBasicInfo($order->user_id);
                $nickname = $userInfo ? $userInfo['nickname'] : '--';

                $merchantName = Merchant::getMerchantName($order->merchant_id);

                /** @var OrderAddress $addressInfo */
                $addressInfo = OrderAddress::getAddressInfo($order->order_address_id);
                $consignee = $addressInfo ? $addressInfo->consignee : '';
                $phone     = $addressInfo ? $addressInfo->phone : '';
                $address   = $addressInfo ? $addressInfo->province . $addressInfo->city . $addressInfo->area . $addressInfo->address : '';

                $deliveryMethodName = Merchant::getDeliveryName($order->delivery_method);

                $orderStatus = \App\Http\Controllers\Api\OrderController::getStatusName($order->status);

                $value = [
                    $order->order_num,$orderStatus,
                    $nickname,$order->pay_amount,
                    $merchantName,$deliveryMethodName,
                    $order->created_at,$order->pay_time,
                    $consignee,$phone,
                    $address,$order->express_time
                ];

                array_push($data,$value);
            }
        }

        return $data;
    }
}
